#include "../../server/mq_queue.hpp"
#include <iostream>
#include <gtest/gtest.h>

using namespace xzt;

MsgQueueManager::ptr p_mqm;

class MsgQueueTest: public testing::Environment
{
    virtual void SetUp() override
    {
        p_mqm = std::make_shared<MsgQueueManager>("./data/queue.db");
    }

    virtual void TearDown() override
    {
        p_mqm->clear();
    }
};


TEST(MsgQueue_test, insert_test)
{
    std::unordered_map<std::string, std::string> map = {{"k1", "v1"}, {"k2", "v2"}};

    p_mqm->declareMsgQueue("MsgQueue1", false, true, false, map);
    p_mqm->declareMsgQueue("MsgQueue2", false, true, false, map);
    p_mqm->declareMsgQueue("MsgQueue3", false, true, false, map);
    p_mqm->declareMsgQueue("MsgQueue4", false, true, false, map);

    ASSERT_EQ(p_mqm->size(), 4);
}

TEST(MsgQueue_test, select_test)
{
    std::string name = "MsgQueue3";
    MsgQueue::ptr p_mq = p_mqm->selectMsgQueue(name);

    ASSERT_NE(p_mq.get(), nullptr);
    ASSERT_EQ(p_mq->_name, name);
    ASSERT_EQ(p_mq->_exclusive, false);
    ASSERT_EQ(p_mq->_durable, true);
    ASSERT_EQ(p_mq->_auto_delete, false);
    ASSERT_EQ(p_mq->getArgs(), std::string("k1=v1&k2=v2"));
}

TEST(MsgQueue_test, delete_test)
{
    std::string name = "MsgQueue1";
    ASSERT_NE(p_mqm->selectMsgQueue(name), nullptr);

    p_mqm->deleteMsgQueue(name);
    ASSERT_EQ(p_mqm->selectMsgQueue(name), nullptr);
}


int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new MsgQueueTest);
    RUN_ALL_TESTS();

    return 0;
}


